package com.nwpusp.display.views.components;

import java.util.Timer;
import java.util.TimerTask;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import com.example.petssports.R;
import com.nwpusp.display.GlobalVar;
import com.nwpusp.display.MainActivity;
import android.graphics.Matrix; 


public class CountDown extends View{
	private MainActivity mainActivity;
	private Bitmap count1Bitmap;
	private Bitmap count2Bitmap;
	private Bitmap count3Bitmap;
	private Bitmap retBitmap;
	private Bitmap countgoBitmap;
	public float x;
	public float y;
	public float scale;
	public float width;
	public float height;
	private int moveStep;
	private Timer timer;
	private TimerTask task;
	private long temp = 0;
	private long StartTime = 0;
	private long SysTime = 0;
	private long timeDiff = 0;
	private int second = 0;
	private boolean sendSoundFlag1 = false;
	private boolean sendSoundFlag2 = false;
	private boolean sendSoundFlag3 = false;
	private boolean sendSoundFlag4 = false;
	
	public CountDown(MainActivity mainActivity){
		super(mainActivity);
		this.mainActivity = mainActivity;
	}
	
	public void setScale(float scale) {
		this.scale = scale;
		getBitmapSource();
		count1Bitmap = GlobalVar.VerticalScale(count1Bitmap, scale);
		count2Bitmap = GlobalVar.VerticalScale(count2Bitmap, scale);
		count3Bitmap = GlobalVar.VerticalScale(count3Bitmap, scale);
		countgoBitmap = GlobalVar.VerticalScale(countgoBitmap, scale);
		width = count1Bitmap.getWidth();
		height = count1Bitmap.getHeight();
		y = (GlobalVar.screenHeight - height)*GlobalVar.half;
		moveStep = GlobalVar.screenWidth/20;
	}
	
	public void startTimer(){
		timer = new Timer();
		task = getNewTimerTask();
		timer.schedule(task,0, 10);
		StartTime = System.nanoTime();
	}
	
	public void stopTimer(){
		timer.cancel();
	}
	
	 public TimerTask getNewTimerTask(){
		TimerTask task = new TimerTask(){  
			public void run() {
				SysTime = System.nanoTime();
				timeDiff = SysTime - StartTime;
				temp = timeDiff / 10000000;
				second = (int)temp/100;
				
					switch(second){
						case 1:{
							retBitmap = count3Bitmap;
							GlobalVar.drawCountDown = true;
							setLocation((int)temp-100);
							if(!sendSoundFlag1){
								mainActivity.mhadnler.sendEmptyMessage(GlobalVar.COUNTDOWN);
								sendSoundFlag1 = true;
								x = 0;
							}
							break;
						}
						case 2:{
							retBitmap = count2Bitmap;
							setLocation((int)temp-200);
							if(!sendSoundFlag2){
								mainActivity.mhadnler.sendEmptyMessage(GlobalVar.COUNTDOWN);
								sendSoundFlag2 = true;
								x = 0;
							}
							break;
						}
						case 3:{
							retBitmap = count1Bitmap;
							setLocation((int)temp-300);
							if(!sendSoundFlag3){
								mainActivity.mhadnler.sendEmptyMessage(GlobalVar.COUNTDOWN);
								sendSoundFlag3 = true;
								x = 0;
							}
							break;
						}
						case 4: {
							retBitmap = countgoBitmap;
							setLocation((int)temp-400);
							if(!sendSoundFlag4){
								mainActivity.mhadnler.sendEmptyMessage(GlobalVar.GO);
								mainActivity.everybodyGo();
								sendSoundFlag4 = true;
								x = 0;
							}
							break;
						}
						case 5: {
							GlobalVar.drawCountDown = false;
							stopTimer();
							break;
						}
						default :break;
					}
				}
			
		}; 
		return task;
	}
	
	public void setLocation(int local){
		if(local<10){
			x += moveStep;
			rotate(-45);
		}
		else if((local>=10)&&(local<=90)){
			rotate(0);
			x= (GlobalVar.screenWidth - mainActivity.countDown.width)*GlobalVar.half;
		}
		else if((local>90)&&(local<=100)){
			rotate(45);
			x += moveStep;
		}
		
	}
	
	private void rotate (int degree) {
		 Matrix matrix=new Matrix(); 
	     matrix.postScale(1f, 1f); 
	     matrix.postRotate(degree); 
	     retBitmap = Bitmap.createBitmap(retBitmap,0,0,retBitmap.getWidth(), retBitmap.getHeight(),matrix,true); 
	}
	
	private void getBitmapSource(){
		count1Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.count1);
		count2Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.count2);
		count3Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.count3);
		countgoBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.countgo);
	}
		
	public Bitmap draw(){
		return retBitmap;
	}
}

//countDown_X = (GlobalVar.screenWidth - mainActivity.countDown.width)*GlobalVar.half;
//countDown_Y = (GlobalVar.screenHeight - mainActivity.countDown.height)*GlobalVar.half;
